package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.GroupBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.GroupMapper;
import com.molichuxing.services.infrastructure.dto.response.response.GroupCreateDto;
import com.molichuxing.services.infrastructure.dto.response.response.GroupDto;
import com.molichuxing.services.infrastructure.dto.response.response.GroupModifyDto;
import com.molichuxing.services.infrastructure.service.GroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("groupService")
public class GroupServiceImpl implements GroupService {

    @Resource
    private GroupMapper groupMapper;

    @Override
    public List<GroupDto> getGroupListByParm(Map<String, Object> param) {
        return Convert.toGroupDtoList(groupMapper.getGroupListByParm(param));
    }

    @Override
    public int getGroupCountByParam(Map<String, Object> param) {
        return groupMapper.getCountByParam(param);
    }

    @Override
    public List<GroupDto> getGroupPageListByParm(Map<String, Object> param, int pageNum, int pageSize) {
        return Convert.toGroupDtoList(groupMapper.getGroupPageListByParm(param, pageNum, pageSize));
    }

    @Override
    public int createGroup(GroupCreateDto dto) {
        if (dto != null) {
            GroupBean bean = Convert.toGroupBean(dto);
            int row = groupMapper.createGroup(bean);
            if (row > 0) {
                return bean.getId();
            }
        }
        return 0;
    }

    @Override
    public int modifyGroup(GroupModifyDto dto) {
        return groupMapper.modifyGroup(Convert.toGroupBean(dto));
    }

    @Override
    public GroupDto getGroupById(int id) {
        GroupBean group = groupMapper.getGroupById(id);
        if (group != null) {
            return Convert.toGroupDto(group);
        }
        return null;
    }

    @Override
    public GroupDto getGroupByNameAndDealerId(Integer systemType, Integer dealerId, String groupName) {
        GroupBean bean = groupMapper.getGroupByNameAndDealerId(systemType, dealerId, groupName);
        if (bean != null) {
            return Convert.toGroupDto(bean);
        }
        return null;
    }

}
